package cn.ekuma.epos.datalogic.define;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.imageio.ImageIO;

import cn.ekuma.epos.datalogic.I_DataLogicSystem;

import com.openbravo.pos.base.AppLocal;

public class DataLogicSystemHelper {

	 public static  BufferedImage getResourceAsImage(I_DataLogicSystem dlSystem,String sName) {
	        try {
	            byte[] img = dlSystem.getResource(sName); // , ".png"
	            return img == null ? null : ImageIO.read(new ByteArrayInputStream(img));
	        } catch (IOException e) {
	            return null;
	        }
	    }
	 
	 
	    
	    public static final void setResourceAsProperties(I_DataLogicSystem dlSystem,String sName, Properties p) {
	        if (p == null) {
	        	dlSystem.setResource(sName, 0, null); // texto
	        } else {
	            try {
	                ByteArrayOutputStream o = new ByteArrayOutputStream();
	                p.storeToXML(o, AppLocal.APP_NAME, "UTF8");
	                dlSystem.setResource(sName, 0, o.toByteArray()); // El texto de las propiedades   
	            } catch (IOException e) { // no deberia pasar nunca
	            }            
	        }
	    }
	    
	    public static final Properties getResourceAsProperties(I_DataLogicSystem dlSystem,String sName) {
	        Properties p = new Properties();
	        try {
	            byte[] img = dlSystem.getResourceAsBinary(sName);
	            if (img != null) {
	                p.loadFromXML(new ByteArrayInputStream(img));
	            }
	        } catch (IOException e) {
	        }
	        return p;
	    }
}
